package com.youku.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

public class XmlReader {

	private static final Logger logger = LoggerFactory.getLogger(XmlReader.class);

	private static Map<String, Action> actions;

	private static Map<String, String> properties;

	static {
		actions = new HashMap<>();
		properties = new HashMap<>();
		initData();
	}

	public static String[] getActionValues(String action) {
		return getActionByName(action).getValues();
	}

	public static String[] getActionKeys(String action) {
		return getActionByName(action).getKeys();
	}

	public static Map<String, Action> getActions() {
		return actions;
	}

	private static void initData() {
		SAXReader reader = new SAXReader();
		Element root;
		try {
			root = reader.read(XmlReader.class.getResourceAsStream("/" + getConfigByOs())).getRootElement();
		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}

		for (Object obj : root.elements("action")) {
			Element element = (Element) obj;
			String name = element.attributeValue("name");
			String path = element.elementText("path");
			path = trimBlankField(path);
			String keys = element.elementText("key");
			keys = trimBlankField(keys);
			String values = element.elementText("value");
			values = trimBlankField(values);
			actions.put(name, new Action(path, keys, values));
		}

		for (Object obj : root.elements("property")) {
			Element element = (Element) obj;
			String name = element.attributeValue("name");
			String value = element.attributeValue("value");
			properties.put(name, value);
		}
	}

	private static String getConfigByOs() {
		String os = System.getProperty("os.name");
		String config=null;
		if (StringUtils.startsWithIgnoreCase(os, "windows")) {
			config="app_windows.xml";
			logger.info("检测到系统运行在windows系统上，选择{}配置文件",config);
			return config;
		} else {
			config="app.xml";
			logger.info("检测到系统运行在非windows系统上，选择{}配置文件",config);
		}
		return config;
	}

	private static String trimBlankField(String value) {
		StringBuilder builder = new StringBuilder();
		for (String s : StringUtils.split(value, ',')) {
			if (StringUtils.isNotBlank(s)) {
				builder.append(StringUtils.trim(s)).append(',');
			}
		}

		if (builder.toString().endsWith(",")) {
			builder.deleteCharAt(builder.length() - 1);
		}

		return builder.toString();
	}

	public static String getProperty(String key) {
		return properties.get(key);
	}

	private static Action getActionByName(String name) {
		Action action = actions.get(name);
		Assert.notNull(action, "不存在名字为" + name + "的action");
		return action;
	}

	public static void main(String[] args) {
		// System.out.println(getActionByName("dsp_click"));
		System.out.println(System.getProperty("os.name"));
	}
}
